package cc.mrbird.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
    public static  String ComputationTime(String lastTime)  {
        char[] chars = lastTime.toCharArray();
        StringBuffer number  =  new StringBuffer();
        StringBuffer text  =  new StringBuffer();
        for (char aChar : chars) {
            if (Character.isDigit(aChar)){
                number.append(aChar);
            }else{
                text.append(aChar);
            }
        }
        Integer time = Integer.parseInt(number.toString());
        String substring = text.toString();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Calendar c = Calendar.getInstance();
        if(substring.equals("小时")){
            //过去1小时
            c.setTime(new Date());
            c.add(Calendar.HOUR_OF_DAY, -1*time);
            Date h = c.getTime();
            String h1 = format.format(h);
            System.out.println("过去"+time+"小时："+h1);

            return h1;
        }

        if(substring.equals("天")){
           c.add(Calendar.DATE, - 1*time);
           Date d = c.getTime();
           String day = format.format(d);
           System.out.println("过去"+time+"天："+day);

            return day;
       }
        if(substring.equals("周")){
            //过去1周
            c.setTime(new Date());
            c.add(Calendar.DATE, - 1*7*time);
            Date d = c.getTime();
            String day = format.format(d);
            System.out.println("过去"+time+"周："+day);
            return day;
        }
        if(substring.equals("月")){
            //过去一月
            c.setTime(new Date());
            c.add(Calendar.MONTH, -1*time);
            Date m = c.getTime();
            String mon = format.format(m);
            System.out.println("过去"+time+"月："+mon);
            return mon;
        }
        if(substring.equals("年")){
            //过去一年
            c.setTime(new Date());
            c.add(Calendar.YEAR, -1*time);
            Date m = c.getTime();
            String year = format.format(m);
            System.out.println("过去"+time+"年："+year);
            return year;
        }
        return null;
    }

}
